﻿using System;
using System.Windows.Forms;

using Microsoft.Win32;

namespace PathConv
{
    public partial class Form1 : Form
    {
        KeyboardHook hook = new KeyboardHook();

        public Form1()
        {
            try
            {
                InitializeComponent();
                hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
                hook.RegisterHotKey(PathConv.ModifierKeys.Win, Keys.C);

                sysTrayIcon.Visible = false;
                sysTrayIconShow.Click += new EventHandler(sysTrayIconShow_Click);
                sysTrayIconQuit.Click += new EventHandler(sysTrayIconQuit_Click);

                this.FormClosed += new FormClosedEventHandler(OnFormClosed);
                this.Shown += new EventHandler(OnFormShown);
            }
            catch (Exception)
            {
                Application.Exit();
            }
        }

        private String UnixToWindows(String unix)
        {
            char driveLetter = unix[1];
            String path = unix.Substring(2);
            path = path.Replace('/', '\\');
            return driveLetter + ":" + path;
        }

        private String WindowsToUnix(String windows)
        {
            char driveLetter = windows.ToLower()[0];
            String path = windows.Substring(2);
            path = path.Replace('\\', '/');
            return "/" + driveLetter + path;
        }

        private String convertPath(String path)
        {
            if (path.Length < 3) return path;

            char first = path.ToLower()[0];
            if (first >= 'a' && first <= 'z' && path[1] == ':')
            {
                // Windows path
                path = WindowsToUnix(path);
            }
            else if (first == '/')
            {
                // UNIX path
                path = UnixToWindows(path);
            }
            return path;
        }

        void hook_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            convertButton_Click(sender, e);
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            String path = Clipboard.GetText();
            String converted = convertPath(path);
            Clipboard.SetText(converted);
            if (!path.Equals(converted))
            {
                sysTrayIcon.ShowBalloonTip(1000, "Converted",
                    "Clipboard contents converted: " + converted, ToolTipIcon.Info);
            }
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            Show();
            sysTrayIcon.Visible = false;
        }

        private void Form1_Resize(object sender, System.EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
            {
                Hide();
                sysTrayIcon.Visible = true;
            }
        }

        private void sysTrayIconShow_Click(object sender, EventArgs e)
        {
            notifyIcon1_Click(sender, e);
        }

        private void sysTrayIconQuit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void OnFormClosed(object sender, FormClosedEventArgs e)
        {
            RegistryKey rk = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\alexander.hans\PathConv");
            rk.SetValue("WindowState", WindowState);
        }

        private void OnFormShown(object sender, EventArgs e)
        {
            RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\alexander.hans\PathConv");
            if (rk != null)
            {
                string windowState = (string)rk.GetValue("WindowState");
                if (windowState.Equals(FormWindowState.Minimized.ToString()))
                {
                    WindowState = FormWindowState.Minimized;
                }
            }
        }
    }
}
